<?php

class UserController extends MyController
{	
	function init()
	{
		parent::init();
	}
	
	function actionIndex()
	{	
		$this->redirect(array($this->controller_id.'/view'));
	}
	
	function actionView($page = 1)
	{
		
		$pages = new CPagination;
		$pages->pageSize = $this->website["beperpage"];

		$rows = NULL;
		
		$rows = user::model()->findAll(array(
			"offset" => ($page - 1) * $this->website["beperpage"],
			"limit" => $this->website["beperpage"]
		));
		
		// tong so dong
		$pages->itemCount =  user::model()->count(); 
		
		$this->data['rows']= $rows;		
		$this->data['pages']= $pages;	
		$this->data['page']= $page;	
		
		$this->render('view', $this->data);
	}
	
	function actionAdd($page = 1)
	{	
		$this->action = "Thêm mới người dùng";
		$user = new user;
				
		$this->data["user"] = $user;
		
		$this->data["nam"] = "";
		$this->data["thang"] = "";
		$this->data["ngay"] = "";
			
		$error = 0;
		$this->data["error"] = $error;
		$this->data["page"] = $page;	
		$this->render("form_edit", $this->data);
	}
	
	function actionEdit($user_id = 0, $page = 1)
	{
		$user = NULL;
		$error = 0;
		$message = "";
		
		if ($user_id == 0)
		{
			$this->redirect(array($this->controller_id.'/view', "page"=>$page));
		}
		
		$user = user::model()->findByPk($user_id);
		
		list($nam, $thang, $ngay) = explode(
			"-",
			MyHelper::get_date_parse($user->date_of_birth)
		);
		
		$this->data["user"] = $user;
		
		$this->data["nam"] = $nam;
		$this->data["thang"] = $thang;
		$this->data["ngay"] = $ngay;
		
		$this->data["error"] = $error;
		$this->data["message"] = $message;
		
		$this->action = "Cập nhật thông tin ";; 
		$this->data["user"] = $user;
		
		$this->data["page"] = $page;
		$this->render("form_edit", $this->data);
		
	}

	function actionSave($user_id = 0, $page = 1)
	{
		$user = NULL;
		$error = 0;
		$message = "";
		
		if ($user_id == 0)
		{
			$user = new user;
			
			$password = Yii::app()->getRequest()->getPost("password");
			$repassword = Yii::app()->getRequest()->getPost("repassword");
			
			if ($this->check_valid_password($password, $repassword) == false)
			{
				$error = 1;
				$message .= "- Password: Không so khớp. <br />";
			}
		}
		else
			$user = user::model()->findByPk($user_id);
		
		$user->setAttributes($_POST, false);
			
		$ngay = Yii::app()->getRequest()->getPost("ngay");
		$thang = Yii::app()->getRequest()->getPost("thang");
		$nam = Yii::app()->getRequest()->getPost("nam");
		
		if ($ngay != 0 || $thang != 0 || $nam != 0)
		{
			if (MyHelper::check_valid_date($nam, $thang, $ngay) == false)
			{
				$error = 1;
				$message .= "- Ngày sinh: Không hợp lệ. <br />";
			}
		}
		
		if ($error == 0)
		{
			$user->date_of_birth = $nam."-".$thang."-".$ngay;
			
			if ($user_id == 0)
			{
				$user->password = md5($password);
				$user->create_time = MyHelper::get_date() . " " . MyHelper::get_time();
			}
			else
				$user->update_time = MyHelper::get_date() . " " . MyHelper::get_time();
				
			$user->save();
			$this->redirect(array($this->controller_id.'/view',"page"=>$page));
		}
		else // $error == 1
		{	
			$this->data["user"] = $user;
			$this->data["error"] = $error;
			$this->data["message"] = $message;
			$this->data["page"] = $page;
			
			$this->data["ngay"] = $ngay;
			$this->data["thang"] = $thang;
			$this->data["nam"] = $nam;
			
			$this->render("form_edit", $this->data);
		}
		
	}
	
	function actionEnable($user_id = 0, $group_id = 0, $page = 1)
	{
		$user = user::model()->findByPk($user_id);
		if (! is_null($user))
		{
			$user->enable = 1 - $user->enable;
			$user->save();	
		}
		$this->redirect(array($this->controller_id."/view", "group_id"=>$group_id, "page"=>$page ));
	}
	
	function actionChange_password($user_id, $group_id = 0, $page = 1)
	{
		$this->action = "Đổi Password";
		$this->data["error"] = 0;
		$row = user::model()->findByPk($user_id);
		$this->data["row"] = $row;
		$this->data['group_id'] = $group_id;
		$this->data['page'] = $page;
		$this->render('form_change_password', $this->data);
	}
	
	function actionSave_change_password($user_id, $group_id = 0, $page = 1)
	{	
		$user = user::model()->findByPk($user_id);
		
		$error = 1;
		
		if ( strcmp(md5($_POST["password"]), $user["password"]) != 0 )
		{
			//$this->data["message"] = "Old-Password invalid";
			$this->data["message"] = "<strong>Password</strong> hiện tại không hợp lệ.";
		}
		elseif ( strcmp($_POST["newpassword"], $_POST["repassword"]) !=0 )
		{
			//$this->data["message"] = "New-Password invalid";
			$this->data["message"] = "<strong>Password mới</strong> và <strong>Gõ lại</strong> không hợp lệ.";
		}
		else{
			$user->password = md5(Yii::app()->getRequest()->getPost("newpassword"));
			$user->save();
			//$this->data["message"] = "Password successfully changed";
			$this->data["message"] = "Đổi Password thành công.";
			$error = 0;
		}
		
		$this->data["page"] = $page;
		$this->data['group_id'] = $group_id;
			
		if ($error == 1)
		{
			$this->data["error"] = $error;
			$this->data["row"] = $user;
			$this->render("form_change_password", $this->data);
		}
		else
		{
			$this->render("view_change_success", $this->data);
		}
	}
	
	
	function actionDelete($user_id,$page = 1)
	{
		if (!$this->is_online($user_id))
		{
			$user = user::model()->findByPk($user_id);
			$user->delete();
			
			$rows = user::model()->findAll(array(
				"offset" => ($page - 1) * $this->website["beperpage"],
				"limit" => $this->website["beperpage"]
			));
	
			if (count($rows) == 0)
			{
				if ($page > 1)
					$page --;
				
			}	
		}
		$this->redirect(array($this->controller_id.'/view', "page"=> $page));
	}
	
	
	function actionDelete_multi($page = 1)
	{
		//var_dump(Yii::app()->getRequest()->getPost("cib"));
		
		$id_array = Yii::app()->getRequest()->getPost("cib");
		
		if (!is_null($id_array))
		{			
			foreach($id_array as $id=>$value)
			{
				if ( ! $this->is_online($id))
				{
					$user = user::model()->findByPk($id);
					$user->delete();
				}
			}
		}
		
		$rows = user::model()->findAll(array(
			"offset" => ($page - 1) * $this->website["beperpage"],
			"limit" => $this->website["beperpage"]
		));
		
		if (count($rows) == 0)
		{
			if ($page > 1)
				$page --;
			
		}
		
		$this->redirect(array($this->controller_id.'/view', "page" => $page));
	}
	
	function actionKickout($user_id, $group_id = 0, $page = 1)
	{	
		session::model()->deleteAll(array(
			"condition"=>"user_id = " . $user_id . " AND user_id <> " . $this->user->id
		));
		$this->redirect(array($this->controller_id.'/view',  "group_id"=>$group_id, "page" => $page));
	}
	
	function check_valid_username($username)
	{
		$user = user::model()->findAll(array("condition"=>"username = '" . $username ."'"));
		return (count($user) == 0) && strlen($username) > 0;
	}
	
	function check_valid_password($password, $repassword)
	{
		return $password == $repassword;
	}
	
	function is_online($user_id){
		$session = session::model()->find(array("condition"=>"user_id = " . $user_id));
		return !is_null($session);
	}
}